package com.purong.purong;

import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.support.multidex.MultiDex;
import android.support.multidex.MultiDexApplication;
import android.text.TextUtils;
import android.util.Log;

import com.purong.purong.bean.LoginBean;
import com.purong.purong.util.CommonUtil;
import com.purong.purong.util.SharePreUtil;
import com.tencent.bugly.crashreport.CrashReport;
import com.umeng.socialize.PlatformConfig;
import com.yao.sdk.core.YaoSDK;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;

import cn.jpush.android.api.JPushInterface;


/**
 * Created by Administrator on 2016/7/12.
 */
public class PuRongApp  extends Application {


    @Override
    public void onCreate() {
        super.onCreate();
        if (YaoSDK.getApplication() == null) {
            YaoSDK.init(this);
        }
        JPushInterface.setDebugMode(true);
        JPushInterface.init(this);
        PlatformConfig.setWeixin(Contant.WX_APP_ID, "47a7e78d0ea766cc434e66f4d8a32502");
        PlatformConfig.setQQZone("1106511539", "OO5Q9UVF5pf03aXB");
        initDefArea();

        Context context = getApplicationContext();
        // 获取当前包名
        String packageName = context.getPackageName();
        // 获取当前进程名
        String processName = getProcessName(android.os.Process.myPid());
        // 设置是否为上报进程
        CrashReport.UserStrategy strategy = new CrashReport.UserStrategy(context);
        strategy.setUploadProcess(processName == null || processName.equals(packageName));
        //腾讯bugly崩溃日志,用户ID保存到bugly后台跟踪数据下extraMessage.txt中
        strategy.setCrashHandleCallback(new CrashReport.CrashHandleCallback() {
            public Map<String, String> onCrashHandleStart(int crashType, String errorType,
                                                          String errorMessage, String errorStack) {
                LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
                if (getLoginInfo(getApplicationContext())!=null){
                    map.put("userId", getLoginInfo(getApplicationContext()).getUser_id()+"");
                }else {
                    map.put("userId", "未登录");

                }
                return map;
            }

            @Override
            public byte[] onCrashHandleStart2GetExtraDatas(int crashType, String errorType,
                                                           String errorMessage, String errorStack) {
                try {
                    return "Extra data.".getBytes("UTF-8");
                } catch (Exception e) {
                    return null;
                }
            }

        });
        CrashReport.initCrashReport(getApplicationContext(), "85040a22dd", true,strategy);
    }
    private void initDefArea(){
        if (SharePreUtil.getInstance(this).getLoactionAreaId()==null){
            SharePreUtil.getInstance(this).setLoactionAreaId("2");
            SharePreUtil.getInstance(this).setLoactionParentId("1");
            SharePreUtil.getInstance(this).setLoactionAreaName("北京");
        }
    }
    /**
     * 获取进程号对应的进程名
     *
     * @param pid 进程号
     * @return 进程名
     */
    private static String getProcessName(int pid) {
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader("/proc/" + pid + "/cmdline"));
            String processName = reader.readLine();
            if (!TextUtils.isEmpty(processName)) {
                processName = processName.trim();
            }
            return processName;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException exception) {
                exception.printStackTrace();
            }
        }
        return null;
    }



    public static LoginBean getLoginInfo(Context context) {
        return LoginBean.read(context);
    }

//    @Override
//    protected void attachBaseContext(Context base) {
//        super.attachBaseContext(base);
//        MultiDex.install(this);
//    }

}
